package com.neusoft.chess.factory;

import com.neusoft.chess.*;
import com.neusoft.chess.myenum.ChessType;

import java.lang.reflect.Constructor;

/**
 * @Author: duan
 * @Date: Create in 18:56 2020/9/28
 * 工厂模式
 */
public class ChessFactory {

    public static Chess createChess(ChessType type, int px, int player) {
        switch (type) {
            case CHE:
                return new CheChess(px, player);
            case MA:
                return new MaChess(px, player);
            case XIANG:
                return new XiangChess(px, player);
            case SHI:
                return new ShiChess(px, player);
            case BOSS:
                return new BossChess(px, player);
            case BING:
                return new BingChess(px, player);
            case PAO:
                return new PaoChess(px, player);
        }

        return null;
    }

    /**
     *
     * @param packageName 类的包名
     * @param className 类的名称
     * @param px    x轴的坐标
     * @param player    阵营
     * @return
     */
    public static Chess createChess(String packageName, String className, int px, int player) {
        try {
            //获取类的模板
            Class cls = Class.forName(packageName + className);
            //获取构造方法对象
            Constructor c = cls.getDeclaredConstructor(int.class, int.class);
            return (Chess) c.newInstance(px, player);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
